﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.IService;
using WsdShop.Model;
using SqlSugar;
using WsdShop.Model.Enum;

namespace WeChat.Controllers
{
    public class UserCenterController : BaseController
    {
        private ICouponService _couponService;
        private ICommonAreaService _areaService;
        private IOrderService _orderService;
        private IExchangeService exchangeservice;
        private ICarouselService carouselservice;
        public UserCenterController(IMemberService s, ICouponService couponService, ICommonAreaService areaService, IOrderService orderService, IExchangeService _exchangeservice, ICarouselService _carouselservice) : base(s)
        {
            _couponService = couponService;
            _areaService = areaService;
            _orderService = orderService;
            exchangeservice = _exchangeservice;
            carouselservice = _carouselservice;
        }

        #region 会员信息
        public ActionResult Index()
        {
            var model = _memberService.GetV_Member(CurrentLoginUser.Id);

            ViewBag.CouponCount = _couponService.GetMyCouponCount(CurrentLoginUser.Id);
            return View(model);
        }

        public ActionResult MemberInfo()
        {
            var model = _memberService.GetV_Member(CurrentLoginUser.Id);
            return View(model);
        }


        public JsonResult GetMemberInfo()
        {
            Result result = new Result();
            try
            {
                result.data= _memberService.GetV_Member(CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        public JsonResult GetMemberLevel()
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.GetMemberLevelList();
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        #endregion

        #region 我的地址
        public ActionResult MyAddress()
        {
            var model = _memberService.GetMemberAdressList(CurrentLoginUser.Id);
            return View(model);
        }

        public JsonResult GetMyAddress()
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.GetMemberAdressList(CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddressInfo()
        {
            return View();
        }

        public JsonResult GetAddressInfo(int id)
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.GetV_MemberAddress(id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDefaultAddress()
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.GetDefaultAddress(CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //删除地址
        public JsonResult DelAddress(int id)
        {
            Result result = new Result();
            try
            {
                _memberService.DelMemberAddress(id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //设为默认地址
        public JsonResult SetDefault(int id)
        {
            Result result = new Result();
            try
            {
                var model = _memberService.GetMemberAddress(id);
                model.IsDefault = true;
                _memberService.EditMemberAddress(model);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //保存地址
        public JsonResult SaveAddress(MemberAddress model)
        {
            Result result = new Result();
            try
            {
                if (!model.Mobile.IsMobile())
                {
                    result.msg = "请输入正确的手机号";
                    return Json(result, JsonRequestBehavior.AllowGet);
                }

                if (model.Id > 0)
                {
                    var address = _memberService.GetMemberAddress(model.Id);
                    address.Address = model.Address;
                    address.IsDefault = model.IsDefault;
                    address.Province = model.Province;
                    address.City = model.City;
                    address.District = model.District;

                    address.Consignee = model.Consignee;
                    address.Mobile = model.Mobile;

                    _memberService.EditMemberAddress(address);
                }
                else
                {
                    model.MemberId = CurrentLoginUser.Id;
                    _memberService.AddMemberAddress(model);
                }
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public JsonResult GetArea(int pid)
        {
            Result result = new Result();
            try
            {
                result.data = _areaService.GetAreaList(pid);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }


            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 签到/积分
        public ActionResult Sign()
        {
            var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
            ViewBag.SignPoint = _memberService.GetMemberLevelInfo(member.MemberLevel).SignPoint;
            return View(member);
        }


        public JsonResult MemberSign()
        {
            Result result = new Result();
            try
            {
                result.data = _memberService.MemberSign(DateTime.Today, CurrentLoginUser.Id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        public ActionResult PointDetail()
        {
            var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
            ViewBag.Point = member.Points;
            ViewBag.Avatar= member.Avatar;
            var model = _memberService.GetMemberPointDetailList(CurrentLoginUser.Id);
            return View(model);
        }

        /// <summary>
        /// 获取积分商品
        /// </summary>
        /// <param name="Skip"></param>
        /// <param name="Take"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetProductExchange(int Skip,int Take)
        {
            int total = 0;
            var list = exchangeservice.GetProductExchange(Skip, Take, out total);
            return Json(new { Total = total, Results = list });
        }

        [HttpPost]
        public ActionResult GetCarousel()
        {
            Result result = new Result();
            result.data = carouselservice.GetCarousel((int)CarouselTypeEnum.积分商城);
            return Json(result);
        }

        public ActionResult PointOrder()
        {
            return View();
        }

        #endregion

        #region 优惠券
        public ActionResult MyCoupon()
        {
            ViewBag.UList = _couponService.GetMyCouponList(CurrentLoginUser.Id, 1);
            ViewBag.NList = _couponService.GetMyCouponList(CurrentLoginUser.Id, 2);
            ViewBag.GList = _couponService.GetMyCouponList(CurrentLoginUser.Id, 3);
            return View();
        }
        #endregion

        #region 充值
        //我的余额
        public ActionResult BalanceDetail()
        {
            var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
            ViewBag.Avatar = member.Avatar;
            return View();
        }

        public ActionResult GetBalanceDetail()
        {
            Result result = new Result();
            try
            {
                 var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                result.data1 = member.Balance;
                var model = _memberService.GetMemberMoneyDetailList(CurrentLoginUser.Id);
                result.data = model;
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        //充值页
        public ActionResult Recharge()
        {
            return View();
        }

        public JsonResult ToRecharge(decimal money)
        {
            Result result = new Result();
            try
            {
                if (money <= 0)
                {
                    result.msg = "请输入正确的充值金额";
                    return Json(result);
                }

                var member = _memberService.GetMemberInfo(CurrentLoginUser.Id);
                MemberRecharge mr = new MemberRecharge();
                mr.Amount = money;
                mr.MemberId = member.Id;
                mr.Status = WsdShop.Model.Enum.OrderPayStatus.未支付;
                mr.PayType = WsdShop.Model.Enum.PayType.微信支付;
                result.data = _memberService.AddMemberRecharge(mr);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }

        #endregion

        #region 我的订单

        public ActionResult MyOrder()
        {
            return View();
        }

        public JsonResult GetMyOrder()
        {
            Result result = new Result();
            try
            {

                result.data=_orderService.GetMyOrderList(CurrentLoginUser.Id);
                result.data1 = _orderService.GetMyReturnGoodsList(CurrentLoginUser.Id);
                
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result);
        }



        #endregion
    }
}